﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Win8NearMe.Bean;
using System.Collections.Generic;
using Windows.ApplicationModel.Core;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Win8NearMe
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page, OnFriendNearByListener,IChatListener
    {
        MessageData messageData = null;
        List<Person> listPerson = null;
        
        public MainPage()
        {
            this.InitializeComponent();
            NetworkLayer networkLayer = new NetworkLayer();
            networkLayer.setOnFriendNearByListener(this);
            networkLayer.sendLocation("1.23434", "2343");
            listPerson = new System.Collections.Generic.List<Person>();
            //messageData = new MessageData();
            //ItemListView.ItemsSource = messageData.Collection;
        }

        void OnFriendNearByListener.onFriendNearByListener(System.Collections.Generic.List<Person> arrayFriends)
        {
          
            listPerson = arrayFriends;
            ItemListView.ItemsSource = arrayFriends;

            object outValue;

            ServerChat server;
            if (!CoreApplication.Properties.TryGetValue("ServerSocket", out outValue)){
                server = new ServerChat();
                CoreApplication.Properties.Add("ServerSocket", server);
                server.startServer();
            }else{
                server = (ServerChat)outValue;
            }


            server.setIChatListener(this);
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (this.Frame != null)
            {
                this.Frame.Navigate(typeof(PageChat));
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.Frame.Navigate(typeof(PageChat));
        }

        private void ListView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (this.Frame != null)
            {
                this.Frame.Navigate(typeof(PageChat));
            }
        }

        public void receiveMessage(string message)
        {
            
        }
    }
}
